<template>
	<view>
		<uni-section title="">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms ref="floorForm" :modelValue="form" label-position="left" label-width="120px"
					:rules="deviceRules">
					<uni-forms-item label="单位" class="form_item" required name="unitId">
						<uni-data-select v-model="form.unitId" placeholder="请选择单位"
							:localdata="unitList.map(item => ({value: item.id, text: item.unitName}))"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="设备品类" class="form_item" required name="deviceCategory">
						<uni-data-select v-model="form.deviceCategory" placeholder="请选择设备品类"
							:localdata="deviceCategoryList.map(item => ({value: item.dictValue, text: item.dictLabel}))"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="设备名称" class="form_item" required name="deviceName">
						<uni-data-select v-model="form.deviceName" placeholder="请选择设备名称"
							:localdata="deviceNameList.map(item => ({value: item.dictValue, text: item.dictLabel}))"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="SN码" class="form_item" required name="deviceCode">
						<uni-easyinput v-model="form.deviceCode" placeholder="请输入SN" />
					</uni-forms-item>
					<uni-forms-item label="设备安装位置" class="form_item" name="deviceAddress">
						<uni-data-select v-model="form.deviceAddress" placeholder="请选择设备安装位置"
							:localdata="deviceAddressList.map(item => ({value: item.dictValue, text: item.dictLabel}))"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="设备型号" class="form_item" name="floorHeight">
						<uni-easyinput v-model="form.floorHeight" placeholder="请输入设备型号" />
					</uni-forms-item>
					<uni-forms-item label="是否推送" class="form_item" name="deviceStatus"
						style="display: flex;align-items: center;">
						<view>
							<u-switch v-model="form.deviceStatus" size="20" activeValue="1"
								inactiveValue="0"></u-switch>
						</view>
					</uni-forms-item>
					<uni-forms-item label="阈值上限" class="form_item" name="deviceMax" 
						v-if="form.deviceCategory == '1'">
						<uni-easyinput v-model="form.deviceMax" placeholder="请输入阈值上限" />
					</uni-forms-item>
					<uni-forms-item label="阈值下限" class="form_item" name="deviceMin"
						v-if="form.deviceCategory == '1'">
						<uni-easyinput v-model="form.deviceMin" placeholder="请输入阈值下限" />
					</uni-forms-item>
					<uni-forms-item label="备注" class="form_item" name="remark">
						<uni-easyinput v-model="form.remark" placeholder="请输入备注" />
					</uni-forms-item>
				</uni-forms>
				<button type="primary" @click="submit('floorForm')">提交</button>
			</view>
		</uni-section>
	</view>
</template>

<script>
	import {
		getDicts
	} from "@/api/system/dict/data.js"
	import {
		listUnit,
	} from "@/api/system/unit"
	import {
		listFloor,
		getFloor,
		delFloor,
		addFloor,
		updateFloor,
		floorPush
	} from "@/api/system/floor"
	import {
		listBuilding,
	} from "@/api/system/building"
	import config from '@/config'
	import {
		getToken
	} from '@/utils/auth'

	export default {
		data() {
			return {
				unitList: [], //单位list
				deviceCategoryList: [], //设备品类
				deviceNameList: [], //设备名称
				deviceAddressList: [], //设备安装位置
				floorIsRefugeList: [{
					text: '是',
					value: 0
				}, {
					text: '否',
					value: 1
				}],
				form: {
					deviceStatus:'1'
				},
				deviceRules: {
					unitId: {
						rules: [{
							required: true,
							errorMessage: '单位不能为空'
						}]
					},
					deviceName: {
						rules: [{
							required: true,
							errorMessage: '设备名称不能为空'
						}]
					},
					deviceAddress: {
						rules: [{
							required: true,
							errorMessage: '设备位置不能为空'
						}]
					},
					deviceTypeId: {
						rules: [{
							required: true,
							errorMessage: '设备类型不能为空'
						}]
					},
					deviceCode: {
						rules: [{
							required: true,
							errorMessage: 'SN码不能为空'
						}]
					},
					deviceStatus: {
						rules: [{
							required: true,
							errorMessage: '推送状态不能为空'
						}]
					},
				},
			}
		},
		created() {
			this.getUnit()
			this.getDeviceCategory()
			this.getdeviceName()
			this.getdeviceAddress()
		},
		methods: {
			/** 查询单位 */
			getUnit(row) {
				listUnit({
					pageNum: 1,
					pageSize: 999999,
				}).then(response => {
					this.unitList = response.rows
			
				})
			},
			submit(ref) {
				this.$refs['floorForm'].validate().then(res => {
					addFloor(this.form).then(response => {
						this.$modal.msgSuccess("新增成功！");
						this.form = {}
						uni.reLaunch({
							url: '/pages/system/floor/index'
						});
					})
				}).catch(err => {
					console.log('err', err);
				})
			},
			//设备品类
			getDeviceCategory() {
				this.getDicts('device_category').then(response => {
					this.deviceCategoryList = response.data
				})
			},
			//设备名称
			getdeviceName() {
				this.getDicts('device_name').then(response => {
					this.deviceNameList = response.data
				})
			},
			//安装位置
			
			getdeviceAddress() {
				this.getDicts('device_address').then(response => {
					this.deviceAddressList = response.data
				})
			},
		}
	}
</script>

<style>
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.form_item {
		margin-bottom: 15px;
	}
</style>